﻿using System;
using FxCore.Stuff;

namespace FxCore.Base
{
    /// <summary>
    /// 投影坐标系下的点
    /// </summary>
    public class XyPoint
    {
        public double X { get; }

        public double Y { get; }

        public static XyPoint Empty { get; }

        static XyPoint()
        {
            XyPoint.Empty = new XyPoint();
        }

        public XyPoint()
        {
            this.X = 0;
            this.Y = 0;
        }

        public XyPoint(double x, double y)
        {
            this.X = x;
            this.Y = y;
        }

        public static bool operator ==(XyPoint pointA, XyPoint pointB)
        {
            if (pointA == null || pointB == null)
            {
                throw new NullReferenceException();
            }

            return Helper.CompareDouble(pointA.X, pointB.X) && Helper.CompareDouble(pointA.Y, pointB.Y);
        }

        public static bool operator !=(XyPoint pointA, XyPoint pointB)
        {
            return !(pointA == pointB);
        }

        public static XyPoint operator -(XyPoint pointA, XyPoint pointB)
        {
            return new XyPoint(pointA.X - pointB.X, pointA.Y - pointB.Y);
        }

        public static XyPoint operator +(XyPoint pointA, XyPoint pointB)
        {
            return new XyPoint(pointA.X + pointB.X, pointA.Y + pointB.Y);
        }

        public override string ToString()
        {
            return $"投影坐标点:{{X=>{this.X}, Y=>{this.Y}}}";
        }

        public override bool Equals(object obj)
        {
            XyPoint point = (XyPoint) obj;
            if (point == null)
            {
                throw new NullReferenceException();
            }

            return Helper.CompareDouble(this.X, point.X) && Helper.CompareDouble(this.Y, point.Y);
        }

        public override int GetHashCode()
        {
            return this.X.GetHashCode() ^ this.Y.GetHashCode();
        }
    }
}
